<?php
class AdminGroupController  extends Zone_Action{
	public function fields($default=NULL){
		$data = array(
			title 	=> array(
				type 		=> 'CHAR',
				form_label => 'Tên phòng',			
			),
			roles =>array(
				type		=>'LIST',
				value		=> explode(',','login,email,message,yahoo,manage_admin,manage_news,'
					.'task,add_project,add_task,diploma,add_diploma,document,'
					.'add_document,personnel,calendar,add_private_calendar,add_department_calendar,'
					.'add_company_calendar,'
					.'crm,crm_admin,'
					.'asset,manage_asset,contact_company,'
					.'contact_person,contact_company_manage,contact_share'),
				form_label	=>'Quyền'				
			),
			date_updated	=> array(
				type			=> 'NONE',
				default_value	=> mysql_date()
			),
			updated_by_id		=> array(
				type			=> 'NONE',
				default_value	=> get_user_id()
			)
		);
		if( is_array( $default ) ){
			foreach( $data as $k => $config ){
				if( $config['type'] != 'NONE' && isset( $default[$k] ) ){
					$data[$k]['default_value'] = $default[$k];
				}
			}
		}	
		return $data;	
	}

	public function indexAction(){
		$user_id = get_user_id();
		$vars = array();
		$WHERE = array();
		$order_by = " ORDER BY `a`.`ID` DESC";
		
		$current_page = max(getInt('p',1),1);		
		$range_page = max(getInt('rp',15),15);
		
		if( isset($_REQUEST['p']) ){
			$vars['p'] = $current_page;
		}
		
		if( isset($_REQUEST['rp'])){
			$vars['rp'] = $range_page;			
		}
		$limit = "LIMIT ".($current_page-1)*$range_page.",$range_page";
				
		$field_order = array('title','username','date_created');
		foreach( $field_order as $f ){
			if( get('order_by') == $f ){
				$order_by = "ORDER BY $f ". ( get('order_type') == 'desc' ? 'desc' : 'asc');
				$vars['order_by'] = $f;
				if( get('order_type') == 'desc' ){
					$vars['order_by']='desc';
				}
				break;	
			}	
		}		
		
		//tong ban ghi
		$total = $this->Model->fetchOne("SELECT  COUNT(*) FROM `groups`");
		//cau sql index
		$sql = "SELECT `a`.*,`b`.`ID` as `user_id`,`b`.`username` 
			FROM `groups` as `a` left join `users` as `b` on `b`.`ID`=`a`.`created_by_id` 
			WHERE 1=1 $order_by $limit ";
		$posts = $this->Model->fetchAll($sql);
		
		$a = $vars;
		unset($a['p']);
		
		$page = page_ajax('#Admin/Group?'.to_query_configs($a).'&p=', $total , $range_page, $current_page);	
			
		$this->set(array(
			page	=> $page,
			total	=> $total,
			vars	=> $vars,
			posts	=> $posts
		));
	}
	
	public function addAction(){
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields());
			$data = $form->getData();
			
			if( !is_array($data) ){
				return $this->setJSON(array(
					message =>	error($data)
				));
			}	
				
			$data['date_created'] = mysql_date();
			$data['created_by_id'] = get_user_id();
			
			$this->Model->insert('groups',$data);			
			$this->Model->removeCache('users*');
			
			return $this->setJSON(array(
				redirect => "#Admin/Group"
			));
		}
	}
	public function editAction(){
		$group_id = getInt('ID',0);
		$post = $this->Model->fetchRow("SELECT * FROM `groups` WHERE `ID`='$group_id'");		
		if( !$post ){
			return $this->setJSON(array(
				conttent => error('Nhóm không tồn tại hoặc đã bị xóa') 
			));
		}
		
		$this->set('post',$post);		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields($post));
			$data = $form->getData();
			
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));			
			}
			
			$check = $this->Model->update('groups',$data," `ID`='$group_id'");
			$this->Model->removeCache('users*');
			
			return $this->setJSON(array(
				redirect => '#Admin/Group'
			));
		}
	}

	public function deleteAction(){
		$id=getInt("ID",0);
		if( $this->Model->fetchRow("SELECT * FROM `users` 
			WHERE `is_deleted`='no' AND `group_id`='$id'") ){
			return $this->setJSON(array(
				alert	=> 'Không thể xóa nhóm đang có người dùng'
			));	
		}
		
		$this->Model->delete('groups',"`ID`='$id'");
		$this->Model->removeCache('users*');
		
		return $this->setJSON(array(
			redirect	=> '#Admin/Group'
		));
	}
}
?>